home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Drop•MPSR 1.2 / DropShell 2.0 ƒ / DSAppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-04  |  8.6 KB  |  269 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSAppleEvents.c
  5. **
  6. **   Description:    Generic AppleEvent handling routines
  7. **                    
  8. **                    This is the set of routines for handling the required Apple events.
  9. **                    You should NEVER have to modify this file!!!
  10. **                    Simply add code in DSUserProcs to the routines called by these.
  11. **
  12. *******************************************************************************
  13. **                       A U T H O R   I D E N T I T Y
  14. *******************************************************************************
  15. **
  16. **    Initials    Name
  17. **    --------    -----------------------------------------------
  18. **    LDR            Leonard Rosenthol
  19. **    MTC            Marshall Clow
  20. **    SCS            Stephan Somogyi
  21. **    DHN            David Nebinger
  22. **    
  23. *******************************************************************************
  24. **                      R E V I S I O N   H I S T O R Y
  25. *******************************************************************************
  26. **
  27. **      Date        Author    Description
  28. **    ---------    ------    ---------------------------------------------
  29. **    04 Dec 95    DHN        Added call to EventAvail() in _HandleDocs to allow system to update splash window.
  30. **    20 Feb 94    LDR        Modified _HandleDocs to pass item count to preflight & postflight
  31. **    11 Dec 93    SCS        Universal Headers/UPPs (Phoenix 68k/PPC & PPCC)
  32. **                        Skipped System 6 compatible rev of DropShell source
  33. **    11/24/91    LDR        Added a handler for 'pdoc' as per DTS recommendation
  34. **                            This caused some reorg & userProc routine changes
  35. **                            I also created a new common AEVT doc extractor
  36. **                        Cleaned up error handling by adding FailErr
  37. **                        Cleaned up the placement of braces
  38. **                        Added the passing of a userDataHandle to the odoc/pdoc routines
  39. **    10/29/91    SCS        Changes for THINK C 5
  40. **    10/28/91    LDR        Officially renamed DropShell (from QuickShell)
  41. **                        Added a bunch of comments for clarification
  42. **    10/06/91    MTC        Converted to MPW C
  43. **    04/09/91    LDR        Added to Projector
  44. **
  45. ******************************************************************************/
  46.  
  47. #include "DSGlobals.h"
  48. #include "DSUserProcs.h"
  49.  
  50. #include "DSAppleEvents.h"
  51.  
  52. AEEventHandlerUPP    OAPPHandlerUPP, ODOCHandlerUPP, PDOCHandlerUPP, QUITHandlerUPP;
  53.  
  54. /*
  55.     This routine does all initialization for AEM, including the
  56.     creation and then population of the dispatch table.
  57. */
  58. #pragma segment Initialize
  59. pascal void InitAEVTStuff ()  {
  60.     OSErr aevtErr;
  61.  
  62.     aevtErr = noErr;
  63.     
  64.     if ( aevtErr == noErr )    {
  65.         OAPPHandlerUPP = NewAEEventHandlerProc(HandleOAPP);
  66.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication, OAPPHandlerUPP, 0, false );
  67.     }
  68.  
  69.  
  70.     if ( aevtErr == noErr )    {
  71.         ODOCHandlerUPP = NewAEEventHandlerProc(HandleODOC);
  72.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments, ODOCHandlerUPP, 0, false );
  73.     }
  74.  
  75.     if ( aevtErr == noErr )    {
  76.         PDOCHandlerUPP = NewAEEventHandlerProc(HandlePDOC);
  77.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments, PDOCHandlerUPP, 0, false );
  78.     }
  79.  
  80.     if ( aevtErr == noErr )    {
  81.         QUITHandlerUPP = NewAEEventHandlerProc(HandleQuit);
  82.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication, QUITHandlerUPP, 0, false );
  83.     }
  84.  
  85.     if ( aevtErr == noErr )
  86.         InstallOtherEvents ();
  87.  
  88.         
  89.     if ( aevtErr != noErr )
  90.         ;        // report an error if you are so included
  91. }
  92.  
  93.  
  94.  
  95. /*    
  96.     This routine is a utility routine for checking that all required 
  97.     parameters in the Apple event have been used.
  98. */
  99. #pragma segment Main
  100. OSErr GotRequiredParams ( AppleEvent *theAppleEvent ) {
  101.     DescType    typeCode;
  102.     Size        actualSize;
  103.     OSErr        retErr, err;
  104.  
  105.     err = AEGetAttributePtr ( theAppleEvent, keyMissedKeywordAttr,
  106.                     typeWildCard, &typeCode, NULL, 0, &actualSize );
  107.     
  108.     if ( err == errAEDescNotFound )    // we got all the required params: all is ok
  109.         retErr = noErr;
  110.     else if ( err == noErr )
  111.         retErr = errAEEventNotHandled;
  112.     else 
  113.         retErr = err;
  114.     
  115.     return retErr;
  116. }
  117.  
  118. /*
  119.     This is another routine useful for showing debugging info.
  120.     It calls the ErrorAlert routine from DSUtils to put up the 
  121.     error message.
  122.  
  123. */
  124. void FailErr(OSErr err) {
  125.  
  126.     if (err != noErr)
  127.         ErrorAlert(kErrStringID, kAEVTErr, err);
  128. }
  129.  
  130. /*    
  131.     This routine is the handler for the oapp (Open Application) event.
  132.     
  133.     It first checks the number of parameters to make sure we got them all 
  134.     (even though we don't want any) and then calls the OpenApp userProc in QSUserProcs.
  135.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  136. */
  137. #pragma segment Main
  138. pascal OSErr HandleOAPP ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  139. #pragma unused ( handlerRefcon )
  140.     OSErr err;
  141.  
  142.     FailErr(err = GotRequiredParams ( theAppleEvent ));
  143.  
  144.     // let's show the user the splash screen
  145.     ShowWindow(gSplashScreen);
  146.  
  147.     OpenApp ();        // pass it on to the app specific routine
  148.  
  149.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  150.         FailErr(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Opening", 7 ));
  151.     
  152.     return err;
  153. }
  154.  
  155.  
  156. /*    
  157.     This routine is the handler for the quit (Quit Application) event.
  158.     
  159.     It first checks the number of parameters to make sure we got them all 
  160.     (even though we don't want any) and then calls the QuitApp userProc in QSUserProcs.
  161.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  162. */
  163.  
  164. #pragma segment Main
  165. pascal OSErr HandleQuit ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  166. #pragma unused ( handlerRefcon )
  167.     OSErr err;
  168.     
  169.     FailErr( err = GotRequiredParams ( theAppleEvent ));
  170.  
  171.     QuitApp ();        // pass it on to the app specific routine
  172.  
  173.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  174.         FailErr(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Qutting", 7 ));
  175.     
  176.     return err;
  177. }
  178.  
  179.  
  180. /*    
  181.     This routine is the low level processing routine for both the 
  182.     odoc (Open Document) and pdoc (Print Document) events.
  183.     
  184.     This routine is the key one, since this is how we get the list of
  185.     files/folders/disks to process.  The first thing to do is the get the
  186.     list of files, and then make sure that's all the parameters (should be!).
  187.     We then send call the PreflightDocs routine (from DSUserProcs), process
  188.     each file in the list by calling OpenDoc (again in DSUserProcs), and finally
  189.     call PostflightDocs (you know where) and setting a return value.
  190. */
  191. #pragma segment Main
  192. pascal OSErr _HandleDocs ( AppleEvent *theAppleEvent, AppleEvent *reply, Boolean opening ) {
  193. #pragma unused ( reply )
  194. #pragma unused ( handlerRefcon )
  195.     OSErr        err;
  196.     FSSpec        myFSS;
  197.     AEDescList    docList;
  198.     long        index, itemsInList;
  199.     Size        actualSize;
  200.     AEKeyword    keywd;
  201.     DescType    typeCode;
  202.     Handle        userDataHandle;
  203.     EventRecord er;
  204.  
  205.     FailErr(err = AEGetParamDesc ( theAppleEvent, keyDirectObject, typeAEList, &docList ));
  206.     FailErr(err = GotRequiredParams ( theAppleEvent ));
  207.  
  208.     /*    How many items do we have?. */
  209.     /* NOTE: Moved here in DS 2.0 due to requests for this info in preflighter */
  210.     FailErr(err = AECountItems ( &docList, &itemsInList ));
  211.     
  212.     if (PreFlightDocs (opening, itemsInList, &userDataHandle))    {    // let the app do any preflighting it might need
  213.  
  214.         for ( index = 1; index <= itemsInList; index++ ) {
  215.             FailErr(err = AEGetNthPtr ( &docList, index, typeFSS, &keywd, &typeCode,
  216.                     (Ptr) &myFSS, sizeof ( myFSS ), &actualSize ));
  217.     
  218.             OpenDoc( &myFSS, opening, userDataHandle );    // call the userProc
  219.             
  220.             // Give the system some time...
  221.             EventAvail(updateMask,&er);
  222.         }
  223.     
  224.         PostFlightDocs (opening, itemsInList, userDataHandle);    // cleanup time
  225.     }
  226.     else
  227.         err = errAEEventNotHandled;    // tells AEM that we didn't handle it!
  228.         
  229.     FailErr(AEDisposeDesc ( &docList ));
  230.  
  231.     return err;
  232. }
  233.  
  234. /*
  235.     This routine is the handler for the odoc (Open Document) event.
  236.     
  237.     The odoc event simply calls the common _HandleDocs routines, which will
  238.     do the dirty work of parsing the AEVT & calling the userProcs.
  239. */
  240. #pragma segment Main
  241. pascal OSErr HandleODOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  242. #pragma unused ( handlerRefcon )
  243.     
  244.     return (_HandleDocs(theAppleEvent, reply, true));    // call the low level routine
  245. }
  246.  
  247. /*
  248.     This routine is the handler for the pdoc (Print Document) event.
  249.     
  250.     The pdoc event like the odoc simply calls the common _HandleDocs routines
  251. */
  252. #pragma segment Main
  253. pascal OSErr HandlePDOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  254. #pragma unused ( handlerRefcon )
  255.     
  256.     return (_HandleDocs(theAppleEvent, reply, false));    // call the low level routine
  257. }
  258.  
  259. /*    
  260.     This is the routine called by the main event loop, when a high level
  261.     event is found.  Since we only deal with Apple events, and not other
  262.     high level events, we just pass everything onto the AEM via AEProcessAppleEvent
  263. */
  264. #pragma segment Main
  265. pascal void DoHighLevelEvent ( EventRecord *event ) {
  266.  
  267.     FailErr ( AEProcessAppleEvent ( event ) );
  268. }
  269.